<?php

namespace App\Http\Controllers;

use App\Models\PhotoClassify;
use App\Services\PhotoClassifyService;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class PhotoClassifyController extends BaseController
{
    protected $photoClassifyService;

    public function __construct(PhotoClassifyService $photoClassifyService)
    {
        parent::__construct();
        $this->photoClassifyService = $photoClassifyService;
    }

    /**
     * 新增、修改
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/3/14 17:19
     */
    public function store(Request $request) : JsonResponse
    {
        $params = $request->all();
        if (!$this->photoClassifyService->saveData($params)) {
            return $this->error('保存失败!');
        }

        return $this->success('', '保存成功！');
    }

    /**
     * 获取一条信息
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/3/14 17:18
     */
    public function show(Request $request) : JsonResponse
    {
        $id = $request->input('photo_classify_id');
        $data = $this->photoClassifyService->getInfo($id);

        return $this->success($data);
    }

    /**
     * 获取树状图分类
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/3/14 17:16
     */
    public function getList(Request $request) : JsonResponse
    {
        $partCompanyId   = $request->input('part_company_id');
        $photoClassifyId = $request->input('photo_classify_id');

        $data = $this->photoClassifyService->treeList($partCompanyId, $photoClassifyId);

        return $this->success($data);
    }

    /**
     * 单个软删除（软删除相关的全部是Laravel内置操作方法）
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/3/14 17:17
     */
    public function destroy(Request $request) : JsonResponse
    {
        $id = $request->input('photo_classify_id');
        if (!PhotoClassify::query()->where('id', $id)->delete()) {
            return $this->error('删除失败！');
        };

        return $this->success('', '删除成功');
    }

}
